use super::dao::BaseDao;
use crate::db::po::user_group_user::{ActiveModel, Column, Entity, Model};
use sea_orm::{ColumnTrait, DatabaseConnection};
use sea_query::IntoCondition;

#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct Filter {
    pub user_group_id: Option<String>,
    pub user_id: Option<String>,
}

impl IntoCondition for Filter {
    fn into_condition(self) -> sea_query::Condition {
        let mut condition = sea_query::Condition::all();

        if let Some(user_group_id) = self.user_group_id {
            condition = condition.add(Column::UserGroupId.eq(user_group_id));
        }

        if let Some(user_id) = self.user_id {
            condition = condition.add(Column::UserId.eq(user_id));
        }

        condition
    }
}

#[async_trait::async_trait]
pub trait UserGroupUserDao:
    BaseDao<Model = Model, ActiveModel = ActiveModel, Entity = Entity, Column = Column, Filter = Filter>
{
}

#[derive(Clone)]
pub struct DefaultUserGroupUserDao {
    db: DatabaseConnection,
}

impl DefaultUserGroupUserDao {
    pub fn new(db: DatabaseConnection) -> Self {
        Self { db }
    }
}

#[async_trait::async_trait]
impl BaseDao for DefaultUserGroupUserDao {
    type Model = Model;
    type ActiveModel = ActiveModel;
    type Entity = Entity;
    type Column = Column;
    type Filter = Filter;

    fn db(&self) -> &DatabaseConnection {
        &self.db
    }
}

#[async_trait::async_trait]
impl UserGroupUserDao for DefaultUserGroupUserDao {}
